<?php

namespace App\Admin\Controllers;

use App\Facades\AdminC;
use App\Http\Controllers\Controller;
use App\Models\TaskConfig;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\Input;

class TaskConfigController extends Controller
{
    use ModelForm;

    public function index()
    {
        return AdminC::content(function(Content $content){
            $content->header('任务列表');
            $content->description('列表');
            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        return AdminC::grid(TaskConfig::class, function(Grid $grid){
            $grid->column(' ',' ');
            $grid->id('Id');
            $grid->name('任务名称');
            $grid->type('单位类型');
            $grid->task_type('任务类型')->display(function($task_type){
                return $this->task_type_arr[$task_type];
            });
            $grid->action('动作类型')->display(function($action){
                return $this->action_arr[$action];
            });
            $grid->created_at('创建时间');

            $grid->actions(function($actions){
                $actions->disableDelete();
                $actions->append('<a href="/admin/taskconfig/opreat?task_id='.$actions->row->id.'">配置详情</a>');
            });

            $grid->disableRowSelector();
            $grid->disableExport();
        });
    }

    /**
     *  添加任务
     */
    public function create()
    {
        return AdminC::content(function(Content $content){
            $content->header('新增任务');
            $content->description('新增');
            $content->body($this->form());
        });
    }

    public function edit($id){
        return AdminC::content(function(Content $content) use($id){
            $content->header('编辑任务');
            $content->description('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    protected function form()
    {
        return AdminC::form(TaskConfig::class, function(Form $form) {
            $form->display('id','Id');
            $form->text('name','任务名称');
            $form->text('type','单位类型');
            $form->textarea('intro','任务说明');
            $form->select('task_type','任务类型')->options($form->model()->task_type_arr);
            $form->select('action','动作类型')->options($form->model()->action_arr);
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更新时间');
        });
    }

    //操作
    public function opreat()
    {
        $input = Input::all();
        if(isset($input['task_id']) && is_numeric($input['task_id'])) {
            $info = TaskConfig::find($input['task_id'])->toArray();
            $info['config'] = unserialize($info['config']);

            return view('admin.taskconfig_opreat', ['info'=>$info]);
        }
    }

    public function opreatSave()
    {
        $input = Input::all();
        $config = [
            'th' => $input['th'],
            'td' => $input['td']
        ];
        $model = TaskConfig::find($input['task_id']);
        $model->config = serialize($config);
        $res = $model->save();
        if($res){
            return response()->json([
                'code' => 1,
                'msg' => '成功'
            ]);
        }else{
            return response()->json([
                'code' => 0,
                'msg' => '失败'
            ]);
        }
    }

}
